import java.net.DatagramPacket;
import java.util.Random;

public interface ISocket {
    int PORT = 6688;
    int BUFF_SIZE =1024*4;
    String SERVER_ADDRESS = "127.0.0.1";//"129.204.228.200";

    String TAG ="\r\n";

    int LOGIN = 1;
    int GET_TALK_COUNT = 2;
    int GET_TALKS =3;

    int ERROR_ARGS = -1;
    int ERROR_ACCESS =-2;
    int SUCCESS =1;

    void login(DatagramPacket packet, String[] datas);

    /**
     * int到byte[] 由高位到低位
     */
    static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }

    /**
     * byte[]转int
     */
    static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (3 - i) * 8;
            value += (bytes[i] & 0xFF) << shift;
        }
        return value;
    }

   static String webEncrypted(String username,String password){
        StringBuilder sb = new StringBuilder();
        byte x = 0;
        for (int b:username.getBytes() ) {
            b+=x;x++;
            if (b < 0)
                b *= -1;
            sb.append(b*2);
        }
        for (int b:password.getBytes() ) {
            b+=x;x++;
            if (b < 0)
                b *= -1;
            sb.append(b);
        }
        String u_p=sb.toString();
        sb.setLength(0);

        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();

        for (int i = 0; i < u_p.length(); i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }

        String randomStr = sb.toString();
        sb.setLength(0);
        for (int i = 0; i < u_p.length(); i++) {
            sb.append(randomStr.charAt(i));
            sb.append(u_p.charAt(i));
        }
        return sb.toString();
    }
}
